home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Portable Patmos 1.1 / patmos-src / src / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-19  |  2.1 KB  |  79 lines  |  [TEXT/KAHL]

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <sys/param.h>
  5. #include <dirent.h>
  6. #include "crtlocal.h"
  7.  
  8. int getdirentries (int fd, char *buffer, int size, long *actualsize)
  9.     {
  10.     struct dirent *buf = (struct dirent *)buffer;
  11.     int idx = crt_fd_tab[fd].flags&~O_CATALOG;
  12.     OSErr err;
  13.     CInfoPBRec  cPB;
  14.     while (size >= sizeof(struct dirent))
  15.         {
  16.         /* get information about dir */
  17.         cPB.hFileInfo.ioCompletion = (ProcPtr)0L;
  18.         cPB.hFileInfo.ioNamePtr = (StringPtr)&(buf->d_name[0]);
  19.         cPB.hFileInfo.ioVRefNum = crt_ioVRefNum;
  20.         cPB.hFileInfo.ioFDirIndex = ++idx;
  21.         cPB.hFileInfo.ioDirID = crt_fd_tab[fd].fd;
  22.  
  23.         memset(buf, 0, sizeof(struct dirent));
  24.         err = errtran(PBGetCatInfoSync(&cPB)); 
  25.         if (err)
  26.             {
  27.             size = 0;
  28.             if (err != -43) return -1;
  29.             }
  30.         else
  31.             {
  32.             buf->d_fileno = cPB.hFileInfo.ioDirID;    /* file number of entry */
  33.             buf->d_namlen = *buf->d_name;    /* length of string in d_name */
  34.             BlockMove(buf->d_name+1, buf->d_name, buf->d_namlen);
  35.             buf->d_name[buf->d_namlen] = 0;
  36.             buf->d_reclen = buf->d_name-(char *)buf+(buf->d_namlen+1);    /* length of this record */
  37.             buf->d_reclen = ((buf->d_reclen-1)|3)+1;
  38.             size -= buf->d_reclen;
  39.             buf = (struct dirent *)(buf->d_reclen+(char *)buf);
  40.             }
  41.         }
  42.     *actualsize = idx;
  43.     crt_fd_tab[fd].flags = O_CATALOG|idx;
  44.     return ((char *)buf-buffer);
  45.     }
  46.  
  47. int read(int fd, void *buf, unsigned size)
  48.     {
  49.     if ((crt_fd_tab[fd].fd&-256)==100<<8)    /* console */
  50.         {
  51.         return read_console_stream(crt_fd_tab[fd].fd&255, buf, size);
  52.         }
  53.     else if (crt_fd_tab[fd].flags & O_PIPE)
  54.         {
  55.         return readpipe(fd, buf, size);
  56.         }
  57.     else if (crt_fd_tab[fd].flags & O_CATALOG)
  58.         {
  59.         long actual;
  60.         int cnt = getdirentries(fd, buf, size, &actual);
  61.         if (cnt < 0) return cnt;
  62.         return actual;
  63.         }
  64.     else
  65.         {
  66.         int i;
  67.         OSErr err;
  68.         IOParam pb;
  69.         pb.ioCompletion = 0;
  70.         pb.ioRefNum = crt_fd_tab[fd].fd;
  71.         pb.ioBuffer = buf;
  72.         pb.ioReqCount = size;
  73.         pb.ioPosMode = fsAtMark;
  74.         PBReadSync((ParmBlkPtr)&pb);
  75.         err = errtran(pb.ioResult);
  76.         return pb.ioActCount;
  77.         }
  78.     }
  79.